package com.hd123.rumba.gwt.widget2.client.dialog;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.hd123.rumba.gwt.util.client.batchprocess.IProgressPanel;
import com.hd123.rumba.gwt.widget2.client.misc.RProgressBar;

/**
 * 进度对话框
 * 
 * @author LiQi
 * 
 */
public class RProgressDialog extends RDialog implements IProgressPanel {

  /** 消息 */
  private HTML message;

  /** 进度文本 */
  private HTML progressText;

  /** 进度条 */
  private RProgressBar progressBar;

  /** 最小值 */
  private int min;

  /** 最大值 */
  private int max;

  /** 当前进度位置 */
  private int pos;

  /** 是否允许操作者中断。默认为true。 */
  private boolean interruptable = true;

  /** 是否被操作者中断 */
  private boolean interrupted = false;

  public RProgressDialog() {
    super();
    setCaptionText("进度");
    setShowCloseButton(false);
    setWorkingAreaPadding(20);

    VerticalPanel root = new VerticalPanel();

    message = new HTML();
    message.setWidth("300px");
    message.setHeight("50px");
    root.add(message);

    progressText = new HTML();
    progressText.setWidth("300px");
    progressText.setHeight("25px");
    progressText.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
    root.add(progressText);

    progressBar = new RProgressBar();
    progressBar.setWidth("300px");
    root.add(progressBar);

    setWidget(root);

    ButtonConfig buttonConfig = new ButtonConfig("中断", false);
    setButtons(new ButtonConfig[] {
      buttonConfig });
    getButton(buttonConfig).addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        interrupted = Window.confirm("是否中断当前操作？");
      }
    });
  }

  public String getMessage() {
    return message.getHTML();
  }

  public void setMessage(String message) {
    // 前面加一个空格避免全中文字符串和英文字符串高度差异引起的界面跳动
    this.message.setHTML("&nbsp;" + message);
  }

  public float getMin() {
    return min;
  }

  public void setMin(float min) {
    this.min = floatToInt(min);
    progressBar.setMin(this.min);
  }

  public float getMax() {
    return max;
  }

  public void setMax(float max) {
    this.max = floatToInt(max);
    progressBar.setMax(this.max);
  }

  public float getPos() {
    return pos;
  }

  public void setPos(float pos) {
    this.pos = floatToInt(pos);
    refreshProgress();
  }

  public float getPercent() {
    return pos * 1.0f / max * 100f;
  }

  public boolean isInterruptable() {
    return interruptable;
  }

  public void setInterruptable(boolean interruptable) {
    this.interruptable = interruptable;
    setShowButtons(interruptable);
  }

  public boolean isInterrupted() {
    return interrupted;
  }

  public int getPgrTextStyle() {
    return PGRSTYLE_COUNT;
  }

  public void setPgrTextStyle(int style) {
  }

  public void initial() {
    pos = min;
    interrupted = false;
    progressBar.reset();
  }

  public void stepBy(int step) {
    setPos(pos + step);
  }

  public void show() {
    super.show();
    int left = (Window.getClientWidth() - getOffsetWidth()) / 2;
    int top = (Window.getClientHeight() - getOffsetHeight()) / 2;
    setPopupPosition(Window.getScrollLeft() + left, Window.getScrollTop() + top);
  }

  /**
   * 刷新进度显示
   * 
   */
  private void refreshProgress() {
    progressText.setHTML("当前为第" + new Integer(pos + 1).toString() + "个，共"
        + new Integer(max - min).toString() + "个");
    progressBar.setPosition(floatToInt(pos));
  }

  /**
   * float转换为String
   * 
   * @param value
   * @return
   */
  private int floatToInt(float value) {
    return new Integer(Math.round(value)).intValue();
  }

}
